package cn.tedu.dental.user.service.impl;

import cn.tedu.dental.admin.pojo.entity.Doctor;
import cn.tedu.dental.base.exception.ServiceException;
import cn.tedu.dental.base.response.JsonResult;
import cn.tedu.dental.base.response.StatusCode;
import cn.tedu.dental.user.mapper.UserMapper;
import cn.tedu.dental.user.pojo.dto.*;
import cn.tedu.dental.user.pojo.entity.User;
import cn.tedu.dental.user.pojo.vo.UserVO;
import cn.tedu.dental.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public void reg(UserRegDTO userRegDTO) {
        //根据用户名查询用户是否存在
        UserVO userVO = userMapper.selectByUsername(userRegDTO.getUsername());
        //如果不为空,说明用户已存在
        if (userVO != null) {
            //主业务如果遇到了问题,需要抛出业务层异常,而不是直接给前端返回结果
            //Controller层才是给前端返回结果的那一层
            throw new ServiceException(StatusCode.USERNAME_ALREADY_EXISTS);
        }
        //如果为空,新增用户
        User user = new User();
        BeanUtils.copyProperties(userRegDTO, user);
        user.setStatus("10");
        userMapper.insert(user);
    }

    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("用户登录，参数：{}", userLoginParam);
        UserVO userVO = userMapper.selectByUsername(userLoginParam.getUsername());
        if (userVO == null) {
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if (!userVO.getPassword().equals(userLoginParam.getPassword())) {
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return userVO;
    }

    @Override
    public void edit(UserEditParam userEditParam) {
        log.debug("修改用户,userEditParam={}", userEditParam);
        User user = new User();
        BeanUtils.copyProperties(userEditParam, user);
        userMapper.update(user);
    }


    @Override
    public void updateUser(UserQuery userQuery) {
        if (userQuery.getId() == null) {
            throw new ServiceException(StatusCode.DATA_UNEXISTS);
        } else {
            User user = new User();
            BeanUtils.copyProperties(userQuery, user);
            userMapper.update(user);
        }
    }

    @Override
    public void updatePassword(UserPasswordParam userPasswordParam) {
        if (userPasswordParam.getId() == null) {
            throw new ServiceException(StatusCode.DATA_UNEXISTS);
        } else {
            User user = new User();
            BeanUtils.copyProperties(userPasswordParam, user);
            userMapper.updatePassword(user);
        }
    }

    @Override
    public UserVO selectUser(UserQuery userQuery) {
        if (userQuery.getId() != null) {
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(userQuery, userVO);
            return userVO;
        }
        return null;
    }

    @Override
    public UserVO selectUserById(UserQuery userQuery) {
        log.debug("入参UserQuery={}", userQuery);
        UserVO userVO = userMapper.selectUserById(userQuery.getId());
        if (userVO.getId() == null) {
            throw new ServiceException(StatusCode.DATA_UNEXISTS);
        }
        return userVO;
    }

    @Override
    public void saveDoctor(UserSaveParam userSaveParam) {
        User user = new User();
        BeanUtils.copyProperties(userSaveParam, user);

        if (user.getId() == null) {
            //新增用户时要判断该用户名是否存在
            if (userMapper.selectByUsername(user.getUsername()) != null) {
                throw new ServiceException(StatusCode.USERNAME_ALREADY_EXISTS);
            }
            //添加的是用户 对应身份为 10
            user.setRole("10");
            userMapper.insertUser(user);
        } else {
            log.debug("出参,user={}", user);
           userMapper.update(user);
        }
    }

    @Override
    public void deleteUser(Long userId) {
        userMapper.deleteUser(userId);
    }
}
